﻿using FMStudio.Documents.TemplateDocument.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using BigEgg.Framework.Foundation;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BigEgg.Framework.UnitTesting;
using FMStudio.Documents.TemplateDocument.Properties;
using System.Xml.Linq;

namespace FMStudio.Documents.TemplateDocument.Test.Models
{
    [TestClass]
    public class ProjectTemplateContentTest
    {
        [TestMethod]
        public void GeneralProjectTemplateContentTest()
        {
            ProjectTemplateContent content = new ProjectTemplateContent();

            content.Name = "ProjectName.fmproj";
            content.TargetName = "TargetProjectName.fmproj";

            Assert.AreEqual("ProjectName.fmproj", content.Name);
            Assert.AreEqual("TargetProjectName.fmproj", content.TargetName);
            Assert.AreEqual("", content.Validate());
        }

        [TestMethod]
        public void PropertiesWithNotification()
        {
            ProjectTemplateContent content = new ProjectTemplateContent();

            AssertHelper.PropertyChangedEvent(content, x => x.Name, () => content.Name = "ProjectName.fmproj");
            Assert.AreEqual("ProjectName.fmproj", content.Name);

            AssertHelper.PropertyChangedEvent(content, x => x.TargetName, () => content.TargetName = "TargetProjectName.fmproj");
            Assert.AreEqual("TargetProjectName.fmproj", content.TargetName);

            AssertHelper.PropertyChangedEvent(content, x => x.Items, () => content.Items.Add(new Folder("SomeFolder")));
            Assert.AreEqual(1, content.Items.Count);
        }

        [TestMethod]
        public void NameValidationTest()
        {
            ProjectTemplateContent content = new ProjectTemplateContent();

            content.Name = null;
            Assert.IsNull(content.Name);
            Assert.AreEqual(Resources.ProjectNameMandatory, content.Validate("Name"));

            content.Name = "";
            Assert.AreEqual("", content.Name);
            Assert.AreEqual(Resources.ProjectNameMandatory, content.Validate("Name"));

            content.Name = "ProjectName.fmproj";
            Assert.AreEqual("", content.Validate("Name"));
        }

        [TestMethod]
        public void TargetProjectNameValidationTest()
        {
            ProjectTemplateContent content = new ProjectTemplateContent();

            content.TargetName = null;
            Assert.IsNull(content.TargetName);
            Assert.AreEqual(Resources.ProjectTargetNameMandatory, content.Validate("TargetName"));

            content.TargetName = "";
            Assert.AreEqual("", content.TargetName);
            Assert.AreEqual(Resources.ProjectTargetNameMandatory, content.Validate("TargetName"));

            content.TargetName = "TargetProjectName.fmproj";
            Assert.AreEqual("", content.Validate("TargetName"));
        }

        [TestMethod]
        public void SerializationTest()
        {
            ProjectTemplateContent content = new ProjectTemplateContent();

            content.Name = "ProjectName.fmproj";
            content.TargetName = "TargetProjectName.fmproj";
            content.Items.Add(new Folder("SomeFolder"));
            content.Items.Add(new ProjectItem("Item"));

            XElement element = content.ObjectToXElement();
            ProjectTemplateContent newContent = element.XElementToObject<ProjectTemplateContent>();

            Assert.AreEqual(content.Name, newContent.Name);
            Assert.AreEqual(content.TargetName, newContent.TargetName);
            Assert.AreEqual(content.Items.Count, newContent.Items.Count);
        }
    }
}
